<?php
/**
 * Created by PhpStorm.
 * User: zk
 * Date: 2018/10/24
 * Time: 下午1:55
 */
use app\assets\AppAsset;
use app\common\CommonHelper;

$base = Yii::$app->request->baseUrl;
AppAsset::addCss($this,$base . '/app/css/dynamic-detail.css');

?>

<div class="search-box">
    <div class="dropdown" style="margin-left:1.67rem;font-size: 1.3rem;float: left">
        <button type="button" class="btn dropdown-toggle" id="dropdownMenu"
                data-toggle="dropdown">
            酒吧状态
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
            <li role="presentation" >
                <a role="menuitem" tabindex="-1" href="javascript:chooseBarStatus('所有状态',-1)">所有状态</a>
            </li>
            <li role="presentation" >
                <a role="menuitem" tabindex="-1" href="javascript:chooseBarStatus('上架',0)">上架</a>
            </li>
            <li role="presentation" >
                <a role="menuitem" tabindex="-1" href="javascript:chooseBarStatus('下架',1)">下架</a>
            </li>
        </ul>
    </div>

    <div class="dropdown" style="margin-left:1.67rem;font-size: 1.3rem;float: left">
        <button type="button" class="btn dropdown-toggle" id="dropdownMenu2"
                data-toggle="dropdown">
            酒吧类型
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu2">
            <li role="presentation" >
                <a role="menuitem" tabindex="-1" href="javascript:chooseBarType('所有类型',-1)">所有类型</a>
            </li>
            <li role="presentation" >
                <a role="menuitem" tabindex="-1" href="javascript:chooseBarType('清吧',1)">清吧</a>
            </li>
            <li role="presentation" >
                <a role="menuitem" tabindex="-1" href="javascript:chooseBarType('夜店',2)">夜店</a>
            </li>
            <li role="presentation" >
                <a role="menuitem" tabindex="-1" href="javascript:chooseBarType('livehouse',3)"> livehouse</a>
            </li>

        </ul>
    </div>

    <select id="cityID" style="margin-left: 1.67rem;float: left;" onchange="searchFunction()">
        <option value="-1">所在城市</option>
        <option value="179">杭州市</option>
        <?php for($index=0;$index<count($cities);$index++){?>
            <option value="<?= $cities[$index]['city_code']?>"><?= $cities[$index]['city_name']?></option>
        <?php }?>
    </select>

    <div class="datep" style="margin-left: 1.67rem;"><input id="nameId" class="name-input" placeholder="请输入酒吧名"></div>

    <img id="search-img" onclick="searchFunction()" class="search-icon" style="margin-left: 1.67rem;" src="<?=$base?>/app/images/new/rc_btn_search_n.png">

<!--    <div class="button-div" style="margin-left: 3.6rem" onclick="goToDetail()">新增</div>-->

    <div style="clear:both;"></div>
</div>

<div class="search-box" style="margin-top: 0;margin-bottom: 1.67rem">

    <span style="float: left;margin-left:1.67rem;font-size: 16px;line-height: 3.3rem">是否入驻:</span>

    <select id="barClaim" style="float: left" onchange="searchFunction()">
        <option value="-1">全部</option>
        <option value="1">已入驻</option>
        <option value="0">未入驻</option>
    </select>

    <span style="float: left;margin-left:1.67rem;font-size: 16px;line-height: 3.3rem">排序规则:</span>

    <select id="sortType" style="float: left" onchange="searchFunction()">
        <option value="-1">默认</option>
        <option value="0">加入时间倒序</option>
        <option value="1">来过人数倒序</option>
        <option value="2">动态数倒序</option>
    </select>

    <div class="clear"></div>
</div>

<div class="table-box" style="background: none; width:1000px; ">
    <table id="table" style="background: #FFFFFF;"></table>

    <div class="showContent" id="show_content" style="position: absolute;display: none;">
        <span class="content" id="content_id">test</span>
    </div>
</div>

<script>

    //初始化表格设置
    $('#table').bootstrapTable({
        toolbar: '#toolbar',        //工具按钮用哪个容器
        clickToSelect: true,//点击行即可选中单选/复选框
        singleSelect: false,//复选框只能选择一条记录
        search: false,//是否显示右上角的搜索框
        striped: true,      //是否显示行间隔色
        cache: false,            //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        sidePagination: "server", //服务端处理分页
        pagination: true,     //是否显示分页（*）
        sortable: false,           //是否启用排序
        sortOrder: "asc",          //排序方式
        method: 'GET',      //请求方式（*）
        pageNumber:1,      //初始化加载第一页，默认第一页
        pageSize: 10,      //每页的记录行数（*）
//        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        cardView: false,          //是否显示详细视图
        queryParams:setParams,
        uniqueId:'bar_id',
        url:'bar',
        responseHandler:dataProcess,

        columns: [{
            field: 'bar_id',
            align: 'center',
            valign: 'middle',
            title: '商家ID'
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '商家名称',
            formatter:function(value,row,index) {
                console.log(row);
                return '<span style="color: #0000ff" onclick="goToDetail('+row.bar_id+')">'+row.bar_name+'</span>';
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '城市',
            formatter:function(value,row,index) {
                return row.city?row.city.city_name:"无";
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '类型',
            formatter:function(value,row,index) {
                return row.bar_type == 1 ? "清吧" : row.bar_type == 2 ? "夜店" : "livehouse"
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '状态',
            formatter:function(value,row,index) {
                return row.is_deleted == 0 ? "上架" : "下架";
            }
        },{
            field: 'people_count',
            align: 'center',
            valign: 'middle',
            title: '来过人数'
        },{
            field: 'dynamic_count',
            align: 'center',
            valign: 'middle',
            title: '动态数'
        },{
            field: 'weight_value',
            align: 'center',
            valign: 'middle',
            title: '权重数',
            formatter:function(value,row,index) {
                return '<input type="number" style="border:1px solid #F0F0F0;border-radius:6px;width:60px;padding: 6px 12px;" value="'+row.weight_value+'" barid="'+row.bar_id+'" onblur="setWeight(this)">';
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '入驻',
            formatter:function(value,row,index) {
                return row.is_claim == 1?"是":"否";
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '操作',
            formatter:function(value,row,index) {
                var html = "";
                if(row.is_deleted == 0){
                    html = '<div class="button-div" onclick="operateController.operateFunc('+row.bar_id+',1)">下架</div>';
                }else{
                    html = '<div class="button-div" onclick="operateController.operateFunc('+row.bar_id+',0)">上架</div>';
                }
                html += '<div class="button-div" style="margin-left: 10px" onclick="goToAddDynamic('+row.bar_id+')">创动态</div>';
                return html;
            }
        }]
    });

    function dataProcess(res){
        var data = res;
        return {
            'rows':data.rows,
            'total':data.total
        }
    }

    var barStatus = -1;
    var barType = -1;

    function setParams(params){
        if (barStatus != -1)
            params.status = barStatus;

        if(barType != -1)
            params.bar_type = barType;

        var content = $("#nameId").val();
        if (content && content != undefined && content.length > 0)
            params.bar_name = content;

        var cityCode = $("#cityID").val();
        if(cityCode != -1)
            params.city_code = cityCode;

        var barClaim = $("#barClaim").val();
        if(barClaim != -1)
            params.is_claim = barClaim;

        var sortType = $("#sortType").val();
        if(sortType != -1)
            params.sort_type = sortType;

        return params;
    }

    function chooseBarStatus(name,status){
        barStatus = status;
        $("#dropdownMenu").text(name);
        $('#table').bootstrapTable('refreshOptions', {pageNumber: 1});
    }

    function chooseBarType(name,type){
        barType = type;
        $("#dropdownMenu2").text(name);
        $('#table').bootstrapTable('refreshOptions', {pageNumber: 1});
    }

    function searchFunction(){
        $('#table').bootstrapTable('refreshOptions', {pageNumber: 1});
    }

    var operateController = {
        operateApi : "bar",
        //上下架酒吧
        operateFunc:function(bar_id , status){

            if(confirm("确认操作该商家？")){
                var param = {
                    bar_id:bar_id,
                    status:status
                };

                sendAjax(this.operateApi,"POST",param,true,function(res){
                    var data = JSON.parse(res);
                    if(data.code == 0)
                        $('#table').bootstrapTable('refresh');
                });
            }
        }
    };

    function setWeight(view){
//        var barId = $(view).attr("barid");
//        console.log(barId);
        weightController.weightFunc($(view).attr("barid"),$(view).val());
    }

    var weightController = {
        weightApi : "bar",
        weightFunc:function(bar_id , weight){
            var param = {
                bar_id:bar_id,
                weight_value:weight
            };

            sendAjax(this.weightApi,"POST",param,true,function(res){
                var data = JSON.parse(res);
                if(data.code == 0)
                    $('#table').bootstrapTable('refresh');
            });
        }
    };

    function goToDetail(barId){
        window.open("bar-detail?bar_id="+barId);
    }

    function goToAddDynamic(barId){
        window.open("add-dynamic?bar_id="+barId);
    }
</script>

